﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using System.Web.Security;

namespace XMK.Utils
{
    /// <summary>
    /// 字符串帮助类
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// 字符串去除掉html格式部分
        /// </summary>
        /// <param name="str">需要格式化的字符串</param>
        /// <param name="emptyStr">格式化后为空则返回的默认字符串</param>
        /// <returns>格式化后的字符串</returns>
        public static string FormatNoHtml(string str,string emptyStr)
        {
            string regexString = "<(.[^>]*)>";
            if (!string.IsNullOrEmpty(str))
            {
                str = Regex.Replace(str, regexString, "", RegexOptions.IgnoreCase).Trim();                
            }
            if (string.IsNullOrEmpty(str))
            {
                str = emptyStr;
            }
            return str;
        }

        /// <summary>
        /// 对字符串进行MD5加密
        /// </summary>
        /// <param name="unEncriptString">待加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string EncriptStringByMD5(string unEncriptString)
        {
            if (!string.IsNullOrEmpty(unEncriptString))
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(unEncriptString, "MD5");
            }
            return string.Empty;
        }
        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public static string Base64Decode(string decryptString) {
            if (string.IsNullOrEmpty(decryptString))
                return decryptString;
            // 去掉最后一个=号
            if (decryptString.EndsWith("=") && decryptString.Length % 4 > 0)
                decryptString = decryptString.Substring(0, decryptString.Length - 1);
            byte[] decbuff = Convert.FromBase64String(decryptString);
            return System.Text.Encoding.GetEncoding("gb2312").GetString(decbuff);
        }
        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public static string Base64Encode(string encryptString) {
            if (string.IsNullOrEmpty(encryptString))
                return encryptString;
            byte[] encbuff = System.Text.Encoding.GetEncoding("gb2312").GetBytes(encryptString); 
            return Convert.ToBase64String(encbuff);
        }
        #region 字符串的拼接和截断

        /// <summary>
        /// 获取指定分隔符第一次出现以后的子字符串
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="separator">分隔符</param>
        /// <returns>指定分隔符第一次出现以后的子字符串</returns>
        public static string SubstringAfter(string str, string separator)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(separator))
                return string.Empty;
            int _index = str.IndexOf(separator);
            return _index == -1 ? string.Empty : str.Substring(_index + separator.Length);
        }
        #endregion
    }
}
